<template>
	<view class='container'>
		<view class='bg-white'>
			<u-search placeholder="搜索地址" @click="addressSearch" :disabled="true"></u-search>
		</view>
		<map id='map' :scale='map.scale' :show-location='map.showLocation' :longitude='map.longitude'
			:latitude='map.latitude' :width='map.width' :height='map.height'
			:markers='map.markers' @regionchange='mapChange'>
			<cover-image :src="map.iconPath" class="icon-img" v-bind:class="{'icon-img-selected':map.iconSelected}"></cover-image>
		</map>
		<view class="site-list bg-white">
			<view class="item row mt5" v-for="(v,k) in list" @click="returnSiteEditPage(v)">
				<view class="icon-display-area">
					<view class="icon"></view>
				</view>
				<view class="site-information">
					<text class="title sm line1" v-text="v.title"></text>
					<text class="address xs mt5 mb10 line1" v-text="v.address"></text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp()
	var QQMapWX = require('@/libs/qqmap-wx-jssdk.min.js')
	var qqmapsdk = new QQMapWX({
		key: 'P6VBZ-6A4KX-NWN4R-T4XZR-AERQV-X2BWZ'
	})
	import {
		nearInformationQuery
	} from '@/api/user'
	export default {
		data() {
			return {
				detail: '',
				map: {
					longitude: 116.39744281768799,
					latitude: 39.909127239981956,
					showLocation: true,
					iconPath: 'https://image.leyuanyinyong.cn/uploads/images/20210825144503a47099691.png',
					iconSelected:false,
					width: 40,
					height: 40,
					scale: 16
				},
				siteId:null,
				list: [],
				position:{},
				address: {
					title: '',
					address: ''
				},
				checked: 0,
				scrollTop: 0,
				mapStatus: 1 // 控制选择地址时 地图不加载附近列表
			}
		},
		created() {
			uni.setNavigationBarTitle({
				title: '搜索地址'
			})
		},
		onLoad(e) {
			if(e.longitude && e.latitude){
				this.map.longitude = e.longitude;
				this.map.latitude = e.latitude;
				this.position.latitude = e.latitude;
				this.position.longitude = e.longitude;
				this.getAddressList()
			}else{
				this.getAddress()
			}
		},
		methods: {
			getAddress() {
				let that = this
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						that.position.latitude = res.latitude;
						that.position.longitude = res.longitude;
						that.getAddressList()
					}
				})
			},
			getAddressList(s = 0) {
				let that = this
				nearInformationQuery(that.position).then(res =>{
					if(res.status == 0){
						that.list = res.data;
					}
				})
			},
			mapChange(e) {
				let that = this
				clearTimeout(this.timer)
				if (e.type == 'begin') {
					this.map.iconPath = 'https://image.leyuanyinyong.cn/uploads/images/20210825144530e39aa1186.png';
					this.map.iconSelected = true;
				}
				this.timer = setTimeout(() => {
					if (e.type == 'end') {
						that.mapCtx = uni.createMapContext('map')
						that.mapCtx.getCenterLocation({
							success: res => {
								// console.log(res)
								that.position.latitude = res.latitude;
								that.position.longitude = res.longitude;
								if (that.mapStatus) { // 防止地图点击时 进行多次加载
									that.getAddressList(1)
								} else {
									that.mapStatus = 1
								}
							}
						})
						this.map.iconPath = 'https://image.leyuanyinyong.cn/uploads/images/20210825144503a47099691.png';
						this.map.iconSelected = false;
					}
				}, 200)

			},
			bindAddress(index) {
				let list = this.list
				let map = this.map
				map.latitude = list[index].location.lat
				map.longitude = list[index].location.lng
				this.setData({
					map: map,
					checked: index,
					address: list[index],
					mapStatus: 0
				})
			},
			setData(obj) {
				Object.assign(this, obj)
			},
			addressSearch() {
				console.log(1)
				uni.navigateTo({
					url: '/pages/bundle/address-search/address-search'
				})
			},
			returnSiteEditPage(e){
				uni.setStorageSync('now_selected_site_information', JSON.stringify(e));
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	.map,
	map {
		width: auto;
		height: auto;
		position: absolute;
		left: 0;
		top: 46px;
		right: 0;
		bottom: 55%;
	}

	.map {
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.icon-img {
		width: 40rpx;
		height: 60rpx;
		display: block;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}
	.icon-img-selected {
		height: 90rpx;
	}
	.site-list{
		position: absolute;
		bottom: 0;
		height: 55%;
		left: 0;
		right: 0;
		overflow: auto;
		padding: 10rpx 0 10rpx 30rpx;
		
		.item{
			border-bottom: 1px solid #eee;
			&:last-child{
				border-bottom: none;
			}
			
			&:first-child{
				.site-information{
					
					.title{
						color: #4e87fc;
						font-weight: bold;
					}
				}
			}
			
			.icon-display-area{
				width: 6%;
				
				.icon{
					width: 10rpx;
					height: 10rpx;
					border: 8rpx solid #ddd;
					border-radius: 50%;
					// margin: 0 auto;
				}
			}
			
			.site-information{
				width: 94%;
				
				.title{
					width: calc(75%);
					display: block;
				}
				
				.address{
					width: calc(75%);
					display: block;
					color: #8e8e8e;
				}
			}
		}
	}
</style>
